<?php
namespace app\staff\controller;
use think\Db;
use think\Request;
use think\Exception;
use app\common\controller\Common;
class Staff extends Common
{
	protected $model = null;
	protected $keyword_search = ['admin.admin_sn','admin.nkname','admin.phone'];
	protected $order = '';

	public function _initialize()
	{
		parent::_initialize();
        // $this->model = model('Repair');
	}

	/**
	* 员工管理列表
	*/
	public function index()
	{
		if(Request()->isGet()){

			/*@员工视图处理*/
			$view = $this->index_view();
			return $this->fetch($view);
		}
	}
	/**
	* 店长-接口
	*/
	public function shop_list()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			
			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
			$search = $this->keyword_search($search_arr);
			$order = $this->order;
			$where = $this->handleShop($data);
			
			$list = model('admin/Admin')->getStaffList($where, $order, $page, $row, $search);
			// echo Db::getlastsql();
			$list = collection($list)->toArray();
			$count = model('admin/Admin')->getStaffCount($where, $order, $search);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
		}
	}
	/**
	* 营业员/维修员-接口
	*/
	public function sale_list()
	{
		if(Request()->isAjax()){
			$info = model('admin/Admin')->field('password',true)->where('admin_id',session('admin.admin_id'))->find();
			if(!$info){
				$this->error(config('status.ERROR_TWO'));
			}
			$this->success($info);
		}
	}
	/**
	 * 员工排行榜
	 */
	public function rank()
	{
		$model = new \app\staff\model\AdminRank();
		if(Request()->isAjax()){
			$data = Request()->param('');
			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			if(isset($data['order_field']) && $data['order_field'] != ''){
				if($data['order_field'] == 'order_num'){
					$order = 'onum.' . $data['order_field'] .' desc';
				}else{
					$order = 'omoney.' . $data['order_field'] .' desc';
				}
			}else{
				$order = 'onum.order_num desc';
			}
			$where = $this->handleRank($data);
			$list = $model->saleRank($where['admin'], $where['order'], $order, $page, $row);
			$count = $model->saleRankCount($where['admin']);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
		}
		if(Request()->isGet()){
			return $this->fetch('');
		}
	}
	/***************************** 私有 ******************************/
	/**
	* 员工管理视图处理
	*/
	private function index_view()
	{
		$admin_type = session('admin.admin_type');
		switch($admin_type){
			case 0: return 'upper_index';;break;//总部
			case 1: return 'upper_index';;break;//店长
			case 2: return 'lower_index';;break;//营业员
			case 3: return 'repair_index';break;//维修员
		}
	}
	/**
	* 店长-列表数据处理
	*/
	private function handleShop($data)
	{
		$where = [];
		if(session('admin.admin_type') != 0){/*非总部*/
			$where['admin.shop_id'] = session('admin.shop_id');
		}
		/*@部门*/
		if(isset($data['admin_type']) && $data['admin_type'] != ''){
			$where['admin.admin_type'] = $data['admin_type'];
		}else{
			$where['admin.admin_type'] = ['in',[config('status.two_num'),config('status.three_num')]];
		}
		/*@职位*/
		if(isset($data['agid']) && $data['agid'] != ''){
			$where['group.agid'] = $data['agid'];
		}
		return $where;
	}
	/**
	* 员工排行榜数据处理
	*/
	private function handleRank($data)
	{
		$admin = [];
		$order = [];
		if(session('admin.admin_type') != 0){/*非总部*/
			$admin['admin.shop_id'] = session('admin.shop_id');
		}
		/*@部门*/
		if(isset($data['admin_type']) && $data['admin_type'] != ''){
			$admin['admin.admin_type'] = $data['admin_type'];
		}else{
			$admin['admin.admin_type'] = ['in',[config('status.two_num'),config('status.three_num')]];
		}
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$order['order.addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$order['order.addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$order['order.addtime'] = ['lt', strtotime($data['end'])];
		}
		return ['admin'=>$admin,'order'=>$order];
	}

}